package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.entity.BUserInfo;
import com.pipayshop.entity.BUserWithdrawalRecordInfo;
import com.pipayshop.entity.vo.BUserLoginVO;
import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.mapper.BUserInfoMapper;
import com.pipayshop.mapper.BUserWithdrawalRecordInfoMapper;
import com.pipayshop.service.BUserInfoService;
import com.pipayshop.service.UserAccountInfoService;
import com.pipayshop.utils.StringUtil;
import com.pipayshop.utils.TokenUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxb
 * @since 2023-08-09
 */
@Service
public class BUserInfoServiceImpl extends ServiceImpl<BUserInfoMapper, BUserInfo> implements BUserInfoService {

    @Resource
    BUserInfoMapper bUserInfoMapper;
    @Resource
    private UserAccountInfoService accountInfoService;
    @Resource
    BUserWithdrawalRecordInfoMapper bUserWithdrawalRecordInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String login(BUserLoginVO bUserLoginVO) {
        if (bUserLoginVO == null) {
            throw new BusinessException("登录失败");
        }
        String piName = bUserLoginVO.getPiName();
        String passWord = bUserLoginVO.getPassWord();
        // 校验操作
        BUserInfo bUserInfo = bUserInfoMapper.selectOne(new QueryWrapper<BUserInfo>()
                .eq("pi_name", piName)
                .eq("passWord", passWord)
                .eq("status", 0));
        if (bUserInfo == null) {
            throw new BusinessException("登录失败");
        }
        // 登录成功
        // 记录登录时间
        bUserInfoMapper.update(null, new UpdateWrapper<BUserInfo>()
                .eq("pi_name", piName)
                .set("last_login_time", new Date()));
        // 封装token
        return TokenUtil.getToken(piName);
    }


    @Override
    public boolean updatePassWord(BUserLoginVO bUserLoginVO) {
        if (bUserLoginVO == null) {
            throw new BusinessException("修改密码失败");
        }
        int update = bUserInfoMapper.update(null, new UpdateWrapper<BUserInfo>()
                .eq("pi_name", bUserLoginVO.getPiName())
                .eq("status", 0)
                .set("password", bUserLoginVO.getPassWord())
                .set("update_time", new Date()));
        return update > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean userWithDraw(String uid, BigDecimal balance) {
        if (StringUtils.isBlank(uid) || balance == null) {
            throw new BusinessException("invalid_parameter");
        }
        accountInfoService.reduceAccount(uid, balance);
        int update = bUserWithdrawalRecordInfoMapper.insert(new BUserWithdrawalRecordInfo(uid, balance));
        return update > 0;
    }

    @Override
    public PageDataVO selectUserWithdrawalRecord(String uid, Integer page, Integer limit) {

        // 设置分页参数
        Page<BUserWithdrawalRecordInfo> pageRecord = new Page<>(page, limit);

        // 查询分页数据封装到page中
        bUserWithdrawalRecordInfoMapper.selectPage(pageRecord, new QueryWrapper<BUserWithdrawalRecordInfo>()
                .eq("uid", uid)
                .orderByDesc("create_time")
        );
        // 封装数据
        return new PageDataVO((int) pageRecord.getTotal(), pageRecord.getRecords());

    }
}
